package com.example.project.projectspringcloud.utils;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author :xujunbao
 * @ClassName com.inxedu.os.edu.service.user.SmsServiceStub
 * @description
 * @Create Date : 2014年9月22日 下午5:30:39
 */
@Data
public class SmsUtil {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 短信内容 现传对应的短信动态参数
     */
    private String msgContent;

    private String destNumber;

    /**
     * 腾讯云短信模板的id
     */
    private String TPL_ID;

    /**
     * 验证码
     */
    private String count;

    /**
     * 以下是阿里云短信的独特属性(由于阿里云短信不能直接按照模板ID发) 消息内容包含参数   如:   您的验证码是${code}.
     */
    private String messageBody;

    private List<String> parameterValues = new ArrayList<>();

    private String smsType;

    private Map sysConfigs;

    /**
     * 根据后台配置选取短信平台, 进行发送
     *
     * @return
     */
    public String sendmsgPoint() {

        String result = "";
        try {
            // 通过腾讯云短信工具类发送短信
            if ("tengXunSms".equals(smsType)) {
                TencentCloudMessageUtil tm = new TencentCloudMessageUtil((String) sysConfigs.get("smssdkappid"), (String) sysConfigs.get("smsstrAppkey"));
                if (StringUtils.isEmpty((String) sysConfigs.get("content"))) {
                    // 发验证码
                    logger.info("手机号: {} ----- 验证码: {}.", destNumber, count);
                    return result = tm.sendMsgPoint(destNumber, (String) sysConfigs.get("sign"), TPL_ID, count);
                } else if (StringUtils.isEmpty(count)) {
                    // 发验证码
                    return result = tm.sendMsgPoint(destNumber, (String) sysConfigs.get("sign"), TPL_ID, (String) sysConfigs.get("content"));
                } else {
                    return result = tm.sendMsgPoint(destNumber, (String) sysConfigs.get("sign"), TPL_ID, (String) sysConfigs.get("content"), count, "");
                }

            } else if ("enterpriseMessage".equals(smsType)) {
                EnterpriseSmsUtil enterpriseSmsUtil = new EnterpriseSmsUtil((String) sysConfigs.get("sdkappid"), (String) sysConfigs.get("strAppkey"), (String) sysConfigs.get("userId"));
                if (parameterValues.size() > 0) {
                    String[] array = new String[parameterValues.size()];
                    parameterValues.toArray(array);
                    return result = enterpriseSmsUtil.sendmsg(destNumber, (String) sysConfigs.get("sign"), (String) sysConfigs.get("content"), array);
                }
                if (StringUtils.isEmpty(msgContent)) {
                    return result = enterpriseSmsUtil.sendmsg(destNumber, (String) sysConfigs.get("sign"), (String) sysConfigs.get("content"), count);
                } else if (StringUtils.isEmpty(count)) {
                    return result = enterpriseSmsUtil.sendmsg(destNumber, (String) sysConfigs.get("sign"), (String) sysConfigs.get("content"), msgContent);
                } else {
                    return result = enterpriseSmsUtil.sendmsg(destNumber, (String) sysConfigs.get("sign"), (String) sysConfigs.get("content"), msgContent, count);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
